<?php
require_once("../resources/commons.php");
require_once("../data/User.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/Text.php");
require_once("HTML/QuickForm/Checkbox.php");
require_once("HTML/QuickForm/Group.php");
require_once("HTML/QuickForm/Submit.php");
require_once("HTML/QuickForm/Hidden.php");
require_once("admin_commons.php");

/**
 * Invitation form.
 * This form is used to send an invitation to a new user.
 * The user will have then to connect to complete his/her registration.
 */
class InvitePage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;

		$l_loginInput=new HTML_QuickForm_text("email");
		$l_loginInput->setLabel("Insert e-mail :");
		$this->addElement($l_loginInput);
		
		$l_superAdminCheckbox=new HTML_QuickForm_checkbox("superAdmin");
		$l_superAdminCheckbox->setLabel("Super admin rights: ");
		$this->addElement($l_superAdminCheckbox);
		
		$l_financeAdminCheckbox=new HTML_QuickForm_checkbox("financeAdmin");
		$l_financeAdminCheckbox->setLabel("Finance admin rights: ");
		$this->addElement($l_financeAdminCheckbox);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("Send");
		$this->addElement($l_submit);
		
		//The validation rules (either client and server)
		$this->addRule("email","Please provide a valid e-mail address","required",null,"client");
		$this->addRule("email","Please provide a valid e-mail address","email",null,"client");
		$this->addRule("email","Please provide a valid e-mail address","required",null,"server");
		$this->addRule("email","Please provide a valid e-mail address","email",null,"server");
		$this->applyFilter("email","trim");
		$this->applyFilter("email","strtolower");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){
    	checkAdminRights();
    	
    	displayTop("Invitation management",null,null);
    	echo("<p>Invite a new user to join the team:</p>");
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	checkAdminRights();
    	
    	$l_email=$page->exportValue("email");
    	$l_adminOption=$page->exportValue("adminOption");
    	$l_isSuperAdmin=!is_null($page->exportValue("superAdmin"));
    	$l_isFinanceAdmin=!is_null($page->exportValue("financeAdmin"));
    	    	
    	//Check if an account already exists
    	$l_record=User::getFromCustomCriteria("WHERE email=:email",array("email" => $l_email));
    	if(count($l_record)==0){	
	    	//Create temporary user account
	    	$l_user=new User();
			$l_user->setEmail($l_email);
			$l_user->setCreatedDate(date("Y/m/d", time()));
			$l_user->setIsAdmin($l_isSuperAdmin);
			$l_user->setIsFinanceAdmin($l_isFinanceAdmin);
			$l_user->setIsActive(false);
			$l_user->setProfileChangeKey(getLongKey());
			$l_user->setFeedKey(getLongKey());
			$l_user->save();
			
			//The registration URL
			sendInvitation($l_email,$l_user->getProfileChangeKey());
    	}else{
	    	displayTop("Invitation process",null,null);
	    	?>
	    	<p>The account <?=$l_email ?> already exists !</p>
	    	Please <a href="<?=getSelfURL() ?>">try again</a>.
	    	<?php
	        displayBottom();
    	}
    }
}

$controller =new HTML_QuickForm_Controller('InvitationProcess', false);
$controller->addPage(new InvitePage('invitation'));

$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();
?>